package com.fmdrama.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.fmdrama.bean.Profile;
import com.fmdrama.util.JdbcUtil;
import com.fmdrama.util.StringUtils;

public class ProfileDao {
	
	/**
	 * 批量新增
	 * @param profiles
	 * @throws SQLException
	 */
	public void insert(List<Profile> profiles) throws SQLException{
		if(null!=profiles && profiles.size() > 0){
			int len = profiles.size();
			Object[][] params = new Object[len][6];
			for(int i=0; i<len; i++){
				Profile profile = profiles.get(i);
				int index = 0;
				params[i][index++] = profile.getId();
				params[i][index++] = profile.getTitle();
				params[i][index++] = profile.getCategory();
				params[i][index++] = profile.getDoc5();
				params[i][index++] = profile.getView();
				params[i][index++] = profile.getFtime();
			}
			
			String sql = "INSERT INTO t_profile(id, title, category, doc5, view, ftime) VALUES(?,?,?,?,?,?)";
			QueryRunner query = new QueryRunner(true);
			query.batch(JdbcUtil.getConnection(), sql, params);
		}
	}
	
	/**
	 * 批量更新
	 * @param profiles
	 * @throws SQLException
	 */
	public void update(List<Profile> profiles) throws SQLException{
		if(null!=profiles && profiles.size() > 0){
			int len = profiles.size();
			Object[][] params = new Object[len][6];
			for(int i=0; i<len; i++){
				Profile profile = profiles.get(i);
				int index = 0;
				params[i][index++] = profile.getTitle();
				params[i][index++] = profile.getCategory();
				params[i][index++] = profile.getDoc5();
				params[i][index++] = profile.getView();
				params[i][index++] = profile.getFtime();
				params[i][index++] = profile.getId();
			}
			
			String sql = "UPDATE t_profile set title=?, category=?, doc5=?, view=?, ftime=? WHERE id=?";
			QueryRunner query = new QueryRunner(true);
			query.batch(JdbcUtil.getConnection(), sql, params);
		}
	}
	
	/**
	 * 批量新增或更新
	 * @param profiles
	 * @throws SQLException
	 */
	public void insertOrUpdate(List<Profile> profiles) throws SQLException{
		List<Profile> updateList = new ArrayList<Profile>(); //更新列表
		List<Profile> insertList = new ArrayList<Profile>(); //新增列表
		for(Profile profile : profiles){
			Profile oldPro = get(profile.getId());
			if(null != oldPro){
				if(StringUtils.isNotEmpty(oldPro.getTitle())){
					profile.setTitle(oldPro.getTitle());
				}
				if(StringUtils.isNotEmpty(oldPro.getDoc5())){
					profile.setDoc5(oldPro.getDoc5());
				}
				if(oldPro.getView() > 200){
					profile.setView(oldPro.getView());
				}
				updateList.add(profile);
			}else{
				insertList.add(profile);
			}
		}
		insert(insertList);
		update(updateList);
	}
	
	/**
	 * 新增或更新
	 * @param profiles
	 * @throws SQLException
	 */
	public void insertOrUpdate(Profile profile) throws SQLException{
		List<Profile> profiles = new ArrayList<Profile>();
		profiles.add(profile);
		insertOrUpdate(profiles);
	}
	
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Profile get(long id) throws SQLException{
		String sql = "SELECT * FROM t_profile WHERE id=?";
		QueryRunner query = new QueryRunner(true);
		Profile profile = query.query(JdbcUtil.getConnection(), sql, new BeanHandler<Profile>(Profile.class), id);
		return profile;
	}
	
}
